﻿using System;
using BootstrapBlazor.Components;
using iTeamyou.LTS.Core.Models;
using NPOI.SS.Formula.Functions;

namespace iTeamyou.LTS.UI.Models
{
	public class SupplierSearchModel : ITableSearchModel
	{
		public string? IdOrName { get; set; }
        public string? LicenseIdOrName { get; set; }
        public string? Type { get; set; }
        public string? IsLicenseExist { get; set; }
        public DateTimeRangeValue? SearchDate { get; set; }
        public DateTimeOffset? SearchStartDate { get; set; }
        public DateTimeOffset? SearchEndDate { get; set; }

        public IEnumerable<IFilterAction> GetSearches()
        {
            var ret = new List<IFilterAction>();
            if (!string.IsNullOrEmpty(IdOrName))
            {
                ret.Add(new SearchFilterAction(nameof(Supplier.Name), IdOrName));
                ret.Add(new SearchFilterAction(nameof(Supplier.SId), IdOrName));
            }

            if (!string.IsNullOrEmpty(LicenseIdOrName))
            {
                ret.Add(new SearchFilterAction(nameof(SupplierLicense.Name), LicenseIdOrName));
                ret.Add(new SearchFilterAction(nameof(SupplierLicense.LId), LicenseIdOrName));
            }

            if (Type != null)
            {
                ret.Add(new SearchFilterAction(nameof(SupplierLicense.Type), Type));
            }

            if (IsLicenseExist != null)
            {
                ret.Add(new SearchFilterAction(nameof(Supplier.SupplierLicenses.Count), IsLicenseExist));
            }

            if (SearchDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Supplier.CreatedOn), SearchDate.Start, FilterAction.GreaterThanOrEqual));
                ret.Add(new SearchFilterAction(nameof(Supplier.CreatedOn), SearchDate.End, FilterAction.LessThanOrEqual));
            }

            if (SearchStartDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchStartDate.Value, FilterAction.GreaterThanOrEqual));
            }

            if (SearchEndDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchEndDate.Value, FilterAction.LessThanOrEqual));
            }

            return ret;
        }

        /// <summary>
        /// 重置操作
        /// </summary>
        public void Reset()
        {
            IdOrName = null;
            LicenseIdOrName = null;
            Type = null;
            SearchDate = null;
            IsLicenseExist = null;
            SearchStartDate = null;
            SearchEndDate = null;
        }
    }

    public class ProductSearchModel : ITableSearchModel
    {
        public string? IdOrName { get; set; }
        public string? LicenseIdOrName { get; set; }
        public string? Type { get; set; }
        public string? UPC { get; set; }
        public string? Brand { get; set; }
        public string? Category { get; set; }
        public string? IsLicenseExist { get; set; }
        public DateTimeRangeValue? SearchDate { get; set; }
        public DateTimeOffset? SearchStartDate { get; set; }
        public DateTimeOffset? SearchEndDate { get; set; }

        public IEnumerable<IFilterAction> GetSearches()
        {
            var ret = new List<IFilterAction>();
            if (!string.IsNullOrEmpty(IdOrName))
            {
                ret.Add(new SearchFilterAction(nameof(Product.Name), IdOrName));
                ret.Add(new SearchFilterAction(nameof(Product.PId), IdOrName));
            }

            if (!string.IsNullOrEmpty(LicenseIdOrName))
            {
                ret.Add(new SearchFilterAction(nameof(ProductLicense.Name), LicenseIdOrName));
                ret.Add(new SearchFilterAction(nameof(ProductLicense.LId), LicenseIdOrName));
            }

            if (Type != null)
            {
                ret.Add(new SearchFilterAction(nameof(ProductLicense.Type), Type));
            }

            if (SearchDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchDate.Start, FilterAction.GreaterThanOrEqual));
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchDate.End, FilterAction.LessThanOrEqual));
            }

            if (SearchStartDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchStartDate.Value, FilterAction.GreaterThanOrEqual));
            }

            if (SearchEndDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchEndDate.Value, FilterAction.LessThanOrEqual));
            }

            if (UPC != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.UPC), UPC));
            }

            if (Brand != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.Brand), Brand));
            }

            if (Category != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.Category), Category));
            }

            if (IsLicenseExist != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.ProductLicenses.Count), IsLicenseExist));
            }

            return ret;
        }

        /// <summary>
        /// 重置操作
        /// </summary>
        public void Reset()
        {
            IdOrName = null;
            LicenseIdOrName = null;
            Type = null;
            SearchDate = null;
            UPC = null;
            Brand = null;
            Category = null;
            IsLicenseExist = null;
            SearchStartDate = null;
            SearchEndDate = null;
        }
    }

    public class BatchSearchModel : ITableSearchModel
    {
        public string? IdOrName { get; set; }
        public string? LicenseIdOrName { get; set; }
        public string? Type { get; set; }
        public string? IsLicenseExist { get; set; }
        public string? ProductInfo { get; set; }
        public string? StoreName { get; set; }
        public DateTimeRangeValue? SearchDate { get; set; }
        public DateTimeOffset? SearchStartDate { get; set; }
        public DateTimeOffset? SearchEndDate { get; set; }

        public IEnumerable<IFilterAction> GetSearches()
        {
            var ret = new List<IFilterAction>();
            if (!string.IsNullOrEmpty(IdOrName))
            {
                ret.Add(new SearchFilterAction(nameof(Batch.Number), IdOrName));
                ret.Add(new SearchFilterAction(nameof(Batch.OrderNumber), IdOrName));
            }

            if (!string.IsNullOrEmpty(LicenseIdOrName))
            {
                ret.Add(new SearchFilterAction(nameof(BatchLicense.Name), LicenseIdOrName));
                ret.Add(new SearchFilterAction(nameof(BatchLicense.LId), LicenseIdOrName));
            }

            if (!string.IsNullOrEmpty(StoreName))
            {
                ret.Add(new SearchFilterAction(nameof(Batch.StoreName), StoreName));
            }

            if (!string.IsNullOrEmpty(ProductInfo))
            {
                ret.Add(new SearchFilterAction(nameof(Batch.ProductSKU), ProductInfo));
                ret.Add(new SearchFilterAction(nameof(Batch.ProductUPC), ProductInfo));
                ret.Add(new SearchFilterAction(nameof(Batch.ProductName), ProductInfo));
            }

            if (Type != null)
            {
                ret.Add(new SearchFilterAction(nameof(BatchLicense.Type), Type));
            }

            if (SearchDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Batch.CreatedOn), SearchDate.Start, FilterAction.GreaterThanOrEqual));
                ret.Add(new SearchFilterAction(nameof(Batch.CreatedOn), SearchDate.End, FilterAction.LessThanOrEqual));
            }

            if (IsLicenseExist != null)
            {
                ret.Add(new SearchFilterAction(nameof(Batch.BatchLicenses.Count), IsLicenseExist));
            }

            if (SearchStartDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchStartDate.Value, FilterAction.GreaterThanOrEqual));
            }

            if (SearchEndDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchEndDate.Value, FilterAction.LessThanOrEqual));
            }

            return ret;
        }

        /// <summary>
        /// 重置操作
        /// </summary>
        public void Reset()
        {
            IdOrName = null;
            LicenseIdOrName = null;
            Type = null;
            SearchDate = null;
            IsLicenseExist = null;
            SearchStartDate = null;
            SearchEndDate = null;
            ProductInfo = null;
            StoreName = null;
        }
    }

    public class ManufacturerSearchModel : ITableSearchModel
    {
        public string? IdOrName { get; set; }
        public string? LicenseIdOrName { get; set; }
        public ProductType? Type { get; set; }
        public string? LicenseType { get; set; }
        public string? RegisterOrResponsible { get; set; }
        public string? RegisterCode { get; set; }
        public string? IsLicenseExist { get; set; }
        public DateTimeRangeValue? SearchDate { get; set; }
        public DateTimeOffset? SearchStartDate { get; set; }
        public DateTimeOffset? SearchEndDate { get; set; }

        public IEnumerable<IFilterAction> GetSearches()
        {
            var ret = new List<IFilterAction>();
            if (!string.IsNullOrEmpty(IdOrName))
            {
                ret.Add(new SearchFilterAction(nameof(Manufacturer.Name), IdOrName));
                ret.Add(new SearchFilterAction(nameof(Manufacturer.MId), IdOrName));
            }

            if (!string.IsNullOrEmpty(LicenseIdOrName))
            {
                ret.Add(new SearchFilterAction(nameof(ManufacturerLicense.Name), LicenseIdOrName));
                ret.Add(new SearchFilterAction(nameof(ManufacturerLicense.LId), LicenseIdOrName));
            }

            if (!string.IsNullOrEmpty(RegisterOrResponsible))
            {
                ret.Add(new SearchFilterAction(nameof(Manufacturer.Register), RegisterOrResponsible));
                ret.Add(new SearchFilterAction(nameof(Manufacturer.ResponsiblePerson), RegisterOrResponsible));
            }

            if (!string.IsNullOrEmpty(RegisterCode))
            {
                ret.Add(new SearchFilterAction(nameof(Manufacturer.RegisterCode), RegisterCode));
            }

            if (Type != null)
            {
                ret.Add(new SearchFilterAction(nameof(Manufacturer.ProductType), Type));
            }

            if (LicenseType != null)
            {
                ret.Add(new SearchFilterAction(nameof(ManufacturerLicense.Type), LicenseType));
            }
            if (SearchDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Manufacturer.CreatedOn), SearchDate.Start, FilterAction.GreaterThanOrEqual));
                ret.Add(new SearchFilterAction(nameof(Manufacturer.CreatedOn), SearchDate.End, FilterAction.LessThanOrEqual));
            }

            if (IsLicenseExist != null)
            {
                ret.Add(new SearchFilterAction(nameof(Manufacturer.ManufacturerLicenses.Count), IsLicenseExist));
            }

            if (SearchStartDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchStartDate.Value, FilterAction.GreaterThanOrEqual));
            }

            if (SearchEndDate != null)
            {
                ret.Add(new SearchFilterAction(nameof(Product.CreatedOn), SearchEndDate.Value, FilterAction.LessThanOrEqual));
            }

            return ret;
        }

        /// <summary>
        /// 重置操作
        /// </summary>
        public void Reset()
        {
            IdOrName = null;
            LicenseIdOrName = null;
            Type = null;
            LicenseType = null;
            SearchDate = null;
            RegisterOrResponsible = null;
            RegisterCode = null;
            IsLicenseExist = null;
            SearchStartDate = null;
            SearchEndDate = null;
        }
    }
}

